package com.hc;

import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;

/**
 * lambda基础二
 */
public class TestLambda2 {

    @Test
    public void test1(){

        int num = 0;

        /**
         * 匿名内部类的方式:
         */
        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World!"+num);
            }
        };

        r.run();

        System.out.println("-----------------------------");

        /**
         * Lambda表达式的方式:
         */
        Runnable r1 = () -> System.out.println("Hello Lambda!"+num);
        r1.run();
    }

    @Test
    public void test2(){
        Consumer<String> con = (x) -> System.out.println(x);
        con.accept("输出内容:Lambda yyds");
    }

    @Test
    public void test3(){
        Comparator<Integer> com =  (x,y) -> {
            System.out.println("函数式接口");
            return Integer.compare(x,y);
        }; //多条语句需用大括号编写
    }

    @Test
    public void test4(){
        Comparator<Integer> com =  (x,y) -> Integer.compare(x,y);
    }

    @Test
    public void test5(){

        //类型推断------------------
//        String[] strs ;
//        strs={"aaa","bbb","cccc"};


        List<String> list = new ArrayList<>();
        show(new HashMap<>());//可以推断出类型⬇️
        //-------------------------
    }

    public void show(Map<String,Integer> map){

    }


    /**
     * 条件:多一个数进行运算
     */

    @Test
    public void test6(){
        Integer total = operation(100, (x) -> x * x);
        System.out.println(total);

        //加200的操作
        //y 可以 改为 200
        System.out.println(operation(200,(y) -> y + y ));
    }

    public Integer operation(Integer num,MyFun mf){
        return mf.getValue(num);
    };
}
